import user from '../../../utils/user'
import {
    createDayProject
} from '../../../api/app'
// TODO
import {
    getInsuranceEnterprise,
    getInsurePlan,
    searchCompanyDetailInfoApi
} from '../../api/insurance'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // TODO
        isDis1: false,
        isDis2: false,
        enter: '',
        enterId: '',
        insureType: [],
        price: [
            '10',
            '11',
            '12',
            '13',
            '14',
            '15',
            '16',
            '17',
            '18',
            '19',
            '20',
            '21',
            '22',
            '23',
            '24',
            '25',
            '26',
            '27',
            '28',
            '29',
            '30',
        ],
        pricevalue: '',
        tip: {
            index: false,
            class: 'left',
            content: '123123312',
            type: ''
        },
        selectIndex: false,
        tipList: false,
        companyList: [],
        isCarryPopup: 0, // 只有在他等于1的时候提示弹窗才会显示
        gjtc: false,
        gjtext: '',
        gjvalue: [5],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    opengjtc() {
        this.setData({
            gjtc: true
        })
    },
    beforeenter() {
        console.log(111)
        this.setData({
            pricevalue: '15'
        })
    },
    closetc() {
        this.setData({
            gjtc: false
        })
    },
    suregj() {
        this.setData({
            gjtext: this.data.pricevalue + '/小时',
            gjtc: false
        })
    },
    bindChangeprice(val) {
        console.log(this.data.price[val.detail.value[0]])
        this.setData({
            pricevalue: this.data.price[val.detail.value[0]]
        })
    },
    onLoad: function () {
        user.waitLogin().then(() => {
            getInsuranceEnterprise({
                insureCompanyType: 'insure_type_rrb'
            }).then((res) => {
                // this.searchCompanyDetailInfo(user.companyName, 1)
                this.setData({
                    'companyList': res
                })
            })
        })
    },
    // TODO
    async searchCompanyDetailInfo(name, type) {
        let res = await searchCompanyDetailInfoApi({
            throwException: false,
            keyword: name
        })
        console.log(res, 'faysgvfytgsatgyf')
        if (res.district === '静海区' && res.city === '天津市') {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: '非常抱歉，当前方案仅支持(天津市静海区以外的)地区投保，请选择"其他方案"继续投保"',
                complete: (res) => {
                    if (res.confirm) {
                        console.log(111)
                        if (type === 1) {
                            this.setData({
                                isDis1: true
                            })
                        }
                        if (type === 2) {
                            this.setData({
                                isDis2: true
                            })
                        }
                    }
                }
            })
        } else {
            if (type === 1) {
                this.setData({
                    isDis1: false
                })
            }
            if (type === 2) {
                this.setData({
                    isDis2: false
                })
            }
        }
    },
    onEnterInput(event) {
        console.log(event, 'event')
        const data = []
        for (const i in this.data.companyList) {
            console.log(i, this.data.companyList[i])
            // if (this.data.companyList[i].serveCompanyName && this.data.companyList[i].serveCompanyName.indexOf(event.detail.value) !== -1) {

            // }
            data.push(this.data.companyList[i])
        }
        console.log(this.data.enter, 'datadata')
        this.setData({
            'enter': this.data.enter,
            'enterId': this.data.enterId,
            tipList: !this.data.tipList ? data : !this.data.tipList,

        }, () => {
            console.log(this.data.enter, '123tipList')
        })
    },
    // 投保企业管理
    onGoManage() {
        if (!user.isOpenWesure) {
            app.toast('暂未开通微保服务，请联系工作人员开通')
            return
        }
        wx.navigateTo({
            url: '/packageInsurance/pages/insurance-organize/insurance-organize',
        })
    },
    onSelect(event) {
        const data = event.currentTarget.dataset
        console.log(data)
        this.setData({
            tip: {
                index: data.index,
                class: data.index % 2 ? 'right' : 'left'
            },
            selectIndex: data.index
        })
    },
    tapEnter(event) {
        if (event.currentTarget.dataset.isdisabled) {
            wx.showToast({
                title: '已禁用',
                icon: 'none'
            })
            return
        }
        setTimeout(() => {
            this.setData({
                insureType: [],
                'enter': event.currentTarget.dataset.name,
                'enterId': event.currentTarget.dataset.id,
                tipList: false,
                isCarryPopup: this.data.isCarryPopup += 1
            }, () => {
                getInsurePlan({
                    serveCompanyId: this.data.enterId,
                    productType: 0
                }).then(res => {
                    this.setData({
                        insureType: res.map(item => {
                            item.dictLabel = item.industryPlanName.replace('万', '万 ')
                            return item
                        }),
                    })
                })
                console.log(this.data.enter, this.data.enterId, 'enter')
                // TODO
                // this.searchCompanyDetailInfo(this.data.enter, 2)
                this.setData({
                    tip: {
                        class: ''
                    },
                })
            })
        }, 16)
    },
    onSubmit() {
        // TODO
        // if (this.data.isDis1 || this.data.isDis2) {
        //     wx.showModal({
        //         title: '提示',
        //         showCancel: false,
        //         content: '非常抱歉，当前方案仅支持(天津市静海区以外的)地区投保，请选择"其他方案"继续投保"',
        //         complete: (res) => {
        //             console.log(res)
        //             return
        //         }
        //     })
        //     return
        // }
        if (!this.data.enterId) {
            app.toast('请选择投保企业')
            return
        }
        if (this.data.selectIndex === false) {
            app.toast('请选择投保方案')
            return
        }
        if (this.data.pricevalue === '') {
            app.toast('请选择工价')
            return
        }

        createDayProject({
            type: user.insureCompanyType === 'insure002_gsc' ? 5 : 4,
            wesureTypeName: this.data.insureType[this.data.selectIndex].industryPlanName,
            wesureType: this.data.insureType[this.data.selectIndex].wesurePlanCode,
            serveCompanyName: this.data.enter,
            serveCompanyId: this.data.enterId,
            industryPlanId: this.data.insureType[this.data.selectIndex].id,
            wages: this.data.pricevalue
        }).then(res => {
            app.toast('创建项目成功，正在为您跳转')
            setTimeout(() => {
                wx.redirectTo({
                    url: '/packageInsurance/pages/insurance-insure-detail/insurance-insure-detail?id=' + res
                })
            }, 2000)
        })
    },
    onCarryPopup() {
        this.setData({
            isCarryPopup: 2
        })
    }
})